﻿using PPP.ViewModel;
using PPP.ViewModel.WindowModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xceed.Wpf.AvalonDock.Layout;

namespace PPP {
    /// <summary>
    /// LayoutInitializer for initialize the docking manager. 
    /// 
    /// <list type="bullet">
    /// <item>
    /// <term>Author</term>
    /// <description>Sebastian Will</description>
    /// </item>
    /// </list>
    /// 
    /// </summary>
    class LayoutInitializer : ILayoutUpdateStrategy {
        public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer) {
            ////AD wants to add the anchorable into destinationContainer
            ////just for test provide a new anchorablepane 
            ////if the pane is floating let the manager go ahead
            LayoutAnchorablePane destPane = destinationContainer as LayoutAnchorablePane;
            if (destinationContainer != null &&
                destinationContainer.FindParent<LayoutFloatingWindow>() != null)
                return false;
            
             string destPaneName = string.Empty;
             if (anchorableToShow.Content is PreviewViewModel)
                 destPaneName = "PreviewPane";
             else if (anchorableToShow.Content is USBViewModel)
                 destPaneName = "USBPane";
             else if (anchorableToShow.Content is PropertiesViewModel)
                 destPaneName = "PropertiesPane";             
             else
                return false;

            var toolsPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == destPaneName);
            if (toolsPane != null)
            {
                toolsPane.Children.Add(anchorableToShow);
                return true;
            }

            return false;
        }

        


        public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown) {
        }


        public bool BeforeInsertDocument(LayoutRoot layout, LayoutDocument anchorableToShow, ILayoutContainer destinationContainer) {
            return false;
        }

        public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown) {

        }
    }
}
